/* eslint-disable @typescript-eslint/no-unused-vars */
// @ts-nocheck

import { ElMessage } from "element-plus";
import axios from "axios";
import { defineStore } from "pinia";

interface Tree {
  label: string
  children?: Tree[]
}

interface PostState {
  iframe: string;
  modelData: Tree[];
  logs: string[];
  columns: string[];
  queryData: { [key: string]: number | string; }[]
}

export const usePostStore = defineStore('post', {
  state: (): PostState => ({
    iframe: `${location.protocol}//${location.hostname}:31234`,
    modelData: [
      {
        label: 'component',
        children: []
      }, {
        label: 'material',
        children: []
      }, {
        label: 'property',
        children: []
      }
    ],
    logs: [],
    columns: [],
    queryData: []
  }),
  getters: {
    // getAddAge(state) {
    //   return state.age + 100
    // },
    // getAddAge2: (state) => (num: number) => state.age + num,
  },
  actions: {
    // saveName(name: string) {
    //   this.name = name
    // },
    // increment() {
    //   this.age++
    // },
    async getOssFiles(key='studio/') {
      try {
        const { data } = await axios.get(`/oss_file?directory=${key}`)
      } catch (error) {
        ElMessage.error(error.message)
      }
    },
    async importModel(params = {}) {
      try {
        const { data } = await axios.post('/import', params)
        const { success, message } = data;
        if (success) this.modelData = this.modelData.map(item => ({ ...item, children: data.data[item.label].map((c: string) => ({ label: c })) }));
        this.logs.unshift(`${new Date().toString()} - ${message}`);
      } catch (error) {
        ElMessage.error(error.message)
      }
    },
    async queryResult() {
      try {
        const { data } = await axios.get('/query')
        const { columns, success, message } = data;
        if (success) {
          this.columns = columns;
          this.queryData = data.data;
        }
        this.logs.unshift(`${new Date().toString()} - ${message}`);
      } catch (error) {
        ElMessage.error(error.message)
      }
    }
  }
})